import java.util.*;

class Student implements Comparable<Student> {
    public String sname;
    public Integer sno;
    public String smajor;

    Student(int sno, String sname, String smajor) {
        this.sno=sno;
        this.sname=sname;
        this.smajor=smajor;
    }

    @Override
    public int hashCode() {
        return this.sno;
    }

    @Override
    public boolean equals(Object o) {
        Student ro=(Student)o;
        return this.sno.equals(ro.sno);
    }

    @Override
    public int compareTo(Student o) {
        return this.sno.compareTo(o.sno);
    }

    @Override
    public String toString() {
        return String.valueOf(sno)+" "+this.sname+" "+this.smajor;
    }
}

class StudentManager {
    public HashSet<Student> stuList=new HashSet<Student>();

    public void add(Student r) {
        if(stuList.add(r)==false)
            System.out.println("已存在");
        else
            System.out.println("成功");
    }

    public void remove(int no) {
        var s=new Student(no,"","");
        stuList.remove(s);
    }
}

class Tools {
    private HashMap<String, ArrayList<Student>> mStu=new HashMap<String, ArrayList<Student>>();

    Tools(StudentManager sm) {
        for(Student s : sm.stuList) {
            var list=mStu.get(s.smajor);
            if(list==null) {
                list=new ArrayList<Student>();
                mStu.put(s.smajor,list);
            }
            list.add(s);
        }
    }

    void forEach() {
        for(var i : mStu.entrySet()) {
            String smajor=i.getKey();
            var list=i.getValue();
            Collections.sort(list);
            System.out.println(smajor);
            for(var s : list)
                System.out.println(s);
        }
    }
}

class test {
    test() {
        var s1=new Student(2,"招新","积极");
        var s2=new Student(1,"像","积极");
        var s3=new Student(3,"彤彤","约炮");
        var s4=new Student(4,"我","约炮");
        var sm=new StudentManager();
        sm.add(s1);
        sm.add(s2);
        sm.add(s3);
        sm.add(s4);
        sm.remove(4);
        var t=new Tools(sm);
        t.forEach();
    }
}

public class exam1 {
    public static void main(String[] args) {
        new test();
    }
}
